﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace DwarfInterpreter
{
    class DwarfWorld
    {
        const int UPDATE_STATES_INTERVAL = 5000; // 5 seconds

        private List<Dwarf> dwarves;
        private Dictionary<string, bool> worldStates;
        private Timer updateTimer;

        private static DwarfWorld instance;        
        public static DwarfWorld Instance
        {
            get 
            {
                if (instance == null)
                {
                    instance = new DwarfWorld();
                }
                return instance;
            }
        }

        private DwarfWorld()
        {
            dwarves = new List<Dwarf>();

            dwarves.Add(new Dwarf());
            dwarves.Add(new Dwarf());
            dwarves.Add(new Dwarf());
            dwarves.Add(new Dwarf());
            dwarves.Add(new Dwarf());

            worldStates = new Dictionary<string, bool>();
            worldStates.Add("daylight", true);

            TimerCallback tmc = this.onUpdateTimer;
            updateTimer = new Timer(tmc, null, 0, UPDATE_STATES_INTERVAL);
        }

        private void onUpdateTimer(object e)
        {
            ICollection<string> keys = worldStates.Keys;

            for (int i = 0; i < keys.Count; ++i )
            {
                string key = keys.ElementAt(i);
                worldStates[key] = !worldStates[key];
            }
        }

        public Dwarf GetDwarfAt(int index)
        {            
            return dwarves[index];
        }

        public bool GetState(string str)
        {
            return worldStates[str];
        }
    }
}
